Skip to content

remove trailing whitespace from multi-line tuple struct field prefix#5708

Open
ytmimi wants to merge 3 commits into
rust-lang:mainfrom
ytmimi:issue_5703
Open

remove trailing whitespace from multi-line tuple struct field prefix#5708
ytmimi wants to merge 3 commits into
rust-lang:mainfrom
ytmimi:issue_5703

Conversation

@ytmimi

@ytmimi ytmimi commented Mar 7, 2023

Copy link
Copy Markdown
Contributor

Fixes #5703, Fixes #5525, Fixes #5997

visibility modifiers always contain a trailing space after them. If the formatted tuple field needs to be written over multiple lines then the extra space will cause issues.

In the best case the space will offset the type name by an extra space and in the worst case it will lead to a "left behind trailing whitespace" error.

These changes were version gated because they cause breaking formatting changes with existing tests.

r? @calebcartwright

@ytmimi

ytmimi commented Mar 7, 2023

Copy link
Copy Markdown
Contributor Author

Here are the relevant differences between the v1 and the newly added v2 tests:
git diff --no-index tests/target/struct_field_doc_comment.rs tests/target/struct_field_doc_comment_v2.rs

 struct MyTuple(
     #[cfg(unix)] // some comment
-    pub  u64,
-    #[cfg(not(unix))] /*block comment */ pub(crate)  u32,
+    pub u64,
+    #[cfg(not(unix))] /*block comment */ pub(crate) u32,
 );
 
 struct MyTuple(
     /// Doc Comments
     /* TODO note to add more to Doc Comments */
-    pub  u32,
+    pub u32,
     /// Doc Comments
     // TODO note
-    pub(crate)  u64,
+    pub(crate) u64,
 );

@ytmimi

ytmimi commented Mar 7, 2023

Copy link
Copy Markdown
Contributor Author

Just ran the Diff Check Job, and everything looks good ✅

Comment thread src/items.rs Outdated
Comment on lines +1784 to +1903
while context.config.version() == Version::Two && prefix.ends_with(char::is_whitespace) {
// Remove any additional whitespace at the end of the prefix.
// For example if there is a space after a visibility modifier.
prefix.pop();
}

@kevinji kevinji Jul 30, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more straightforward:

Suggested change
while context.config.version() == Version::Two && prefix.ends_with(char::is_whitespace) {
// Remove any additional whitespace at the end of the prefix.
// For example if there is a space after a visibility modifier.
prefix.pop();
}
if context.config.version() == Version::Two {
// Remove trailing whitespace after the prefix, such as a visibility modifier.
prefix.truncate(prefix.trim_end().len());
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for helping to simplify the implementation

@calebcartwright

Copy link
Copy Markdown
Member

I'd be content merging this as-is because it cleanly fixes an obvious and annoying issue (and I'll add I hate we have to gate this, but agree that we unfortunately must).

However, it's typically a smell for me when we have to remove whitespace that we previously added farther back in the call stack. Do you think it would be feasible to apply a different fix that only adds the whitespace conditionally when necessary? And if so, is that something you think we could turn around quickly or would it be better to move forward with this fix and add a tracking issue and/or fixme comment to try that alternative approach?

@ytmimi

ytmimi commented Aug 13, 2023

Copy link
Copy Markdown
Contributor Author

I'll have to revisit this. I can take a look to see what's going on in rewrite_struct_field_prefix to see if we can prevent emitting the trailing space. I imagine that change would touch more files, but I also agree that it would be better not to emit the trailing space in the first place.

@ytmimi

ytmimi commented Aug 31, 2023

Copy link
Copy Markdown
Contributor Author

@calebcartwright check out the latest commit. It removes the trailing whitespace when using Version::Two. The main downside is that we need to make several changes in a lot of different places. Let me know if you think this updated change is better and I'll squash the commits.

Comment thread src/items.rs
// format_visibility doesn't have a trailing space in Version::Two
result.push_str(&visibility);
} else {
result.push_str(visibility.trim());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that there were other places in the codebase that already dealt with trimming the trailing space coming back from format_visibility.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, it feels like a footgun that format_visibility also attaches trailing whitespaces.

Fixes 5703, Fixes 5525

visibility modifiers always contain a trailing space after them. If the
formatted tuple field needs to be written over multiple lines then the
extra space will cause issues.

In the best case the space will offset the type name by an extra space
and in the worst case it will lead to a "left behind trailing
whitespace" error.

@jieyouxu jieyouxu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Excuse the review 3 years later 🦆)

I think the approach taken in this PR is reasonable enough for a fix that IMO is worth landing. A separate follow-up investigation on the handling of format_visibility is likely warranted though, that it attaches a trailing space I agree feels quite sus.

(Though this PR will need a... rebase)
@rustbot author

View changes since this review

@@ -0,0 +1,9 @@
// rustfmt-version: Two

@jieyouxu jieyouxu Mar 3, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: // rustfmt-style_edition: 2027

(Since style edition 2024 is already stable?)

Likewise in other tests.

Comment thread src/imports.rs
Comment on lines +339 to +341
if !vis.is_empty() && context.config.version() == Version::Two {
vis += " ";
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: && context.config.style_edition() >= StyleEdition::Edition2027 I think

Comment thread src/items.rs
Comment on lines +345 to +348
if !vis.is_empty() && context.config.version() == Version::Two {
// format_visibility doesn't have a trailing space in Version::Two
result.push(' ');
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

  • !vis.is_empty() && context.config.version() == Version::Two -> !vis.is_empty() && context.config.style_edition() >= StyleEdition::2027
  • // format_visibility doesn't have a trailing space in Version::Two -> // format_visibility doesn't have a trailing space in StyleEdition::2027

and elsewhere

Comment thread src/items.rs
// format_visibility doesn't have a trailing space in Version::Two
result.push_str(&visibility);
} else {
result.push_str(visibility.trim());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, it feels like a footgun that format_visibility also attaches trailing whitespaces.

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Mar 3, 2026
@rustbot

rustbot commented Mar 3, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@jieyouxu jieyouxu added A-2027-style-edition Area: style edition 2027 F-impacts-stable-formatted-code Expected formatting impact: affects stable formatted code (caution) F-impacts-stable-default-format Expected formatting impact: affects stable default format configuration (caution) labels Mar 3, 2026
@jieyouxu jieyouxu added the F-requires-next-style-edition Expected formatting impact: can only be done over a style edition (caution) label Mar 3, 2026
@rustbot

rustbot commented Mar 13, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #6823) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-2027-style-edition Area: style edition 2027 F-impacts-stable-default-format Expected formatting impact: affects stable default format configuration (caution) F-impacts-stable-formatted-code Expected formatting impact: affects stable formatted code (caution) F-requires-next-style-edition Expected formatting impact: can only be done over a style edition (caution) pr-follow-up-review-pending S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author.

Projects

None yet

5 participants